package com.example.demo.service.user.impl;


import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.R.BanResult;
import com.example.demo.domin.Vo.UserVO;
import com.example.demo.domin.dto.PageDTO;
import com.example.demo.domin.dto.UserDTO;
import com.example.demo.domin.entity.User;
import com.example.demo.domin.query.UserQuery;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.user.IUserService;
import com.example.demo.utils.DetectSkinDiseaseUtil;
import com.example.demo.utils.DifyClient;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.DirectProcessor;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxProcessor;

import java.util.Map;
import java.util.Set;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    private static FluxProcessor<String, String> processor;

    @Override
    @SneakyThrows
    public Set<? extends Map.Entry<String, ?>> load(MultipartFile file) throws Exception {
        // 调用aliyun接口，返回可能病例
        Set<? extends Map.Entry<String, ?>> set = DetectSkinDiseaseUtil.detect(file);
//        List<Sickness> list = new ArrayList<Sickness>();
////        String apiKey = "app-OahH9s3glemmAMV0Sy0SzJRb";
////        DifyClient difyClient = new DifyClient(apiKey);
//        CountDownLatch countDownLatch = new CountDownLatch(3);
//        set.forEach(e->{
//            //调用dify工作流,保留相似度
//            Sickness sickness = Sickness.builder()
//                    .sicknessName(e.getKey())
//                    .similarity((double) e.getValue())
//                    .executorName(null)
//                    .build();
//            list.add(sickness);
//            countDownLatch.countDown();
//        });
//        countDownLatch.await();
        return set;
    }

    @Override
    public PageDTO<UserVO> queryUsersPage(UserQuery query) {
        Page<User> queryMpPage = query.toMpPage();
        page(queryMpPage);
        return PageDTO.of(queryMpPage, UserVO.class);
    }



    @SneakyThrows
    public static Flux<ServerSentEvent<String>> dify(DifyClient difyClient, Map.Entry e){
        processor= DirectProcessor.<String>create().serialize();
        new Thread(()->{
            try {
                difyClient.runWorkflow(e.getKey()+"怎么治");
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }).start();
        return processor.onBackpressureBuffer()
                .map(data -> ServerSentEvent.<String>builder()
                        .data(data)
                        .build());
    }

    public void sendToClients(String data) {
        processor.onNext(data);
    }
    public void stopStream() {
        processor.onComplete(); // 所有客户端收到 "close" 事件
//        System.out.println("结束");
    }
}
